﻿@model Shop.Wares.Models.Wares
@{
    //修改商品信息
    Layout = null;
    ViewBag.EditorCssStyle = "width:100%;height:400px;";
    ViewBag.EditorContents = Html.Raw(Model.Introduce);
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>UpdateWares</title>

    <link href="~/Content/BootStrap/css/bootstrap.min.css" rel="stylesheet" />
    <link href="~/Content/BootStrap/css/bootstrap-theme.min.css" rel="stylesheet" />
    <link href="~/Content/BootStrap/css/docs.min.css" rel="stylesheet" />
    <link href="~/bootstrapvalidator-master/dist/css/bootstrapValidator.min.css" rel="stylesheet" />

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Content/BootStrap/js/bootstrap.min.js"></script>
    <script src="~/Content/BootStrap/js/docs.min.js"></script>
    <script src="~/bootstrapvalidator-master/dist/js/bootstrapValidator.min.js"></script>
    <script src="~/bootstrapvalidator-master/dist/js/language/zh_CN.js"></script>

    @*上传*@
    <script src="~/bootstrap-fileinput-master/js/fileinput.min.js"></script>
    <script src="~/bootstrap-fileinput-master/js/locales/zh.js"></script>
    <link href="~/bootstrap-fileinput-master/css/fileinput.min.css" rel="stylesheet" />
    <style type="text/css">
        #main {
            width: 1000px;
            margin: 50px auto;
        }
    </style>
</head>
<body>
    <div id="main">
        <form id="UpdateForm" method="post" class="form-horizontal" action="UpdateWares" enctype="multipart/form-data">
            <input type="text" hidden="hidden" value="@Model.WID" name="WID" id="WID" />
            <div class="form-group">
                <label class="col-lg-2 control-label" for="WName">商品名</label>
                <div class="col-lg-8">
                    <input type="text" class="form-control" name="WName" id="WName" placeholder="商品名" value="@Model.WName" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="CID">类别</label>
                <div class="col-lg-4">
                    <select class="form-control" name="CID" id="CID">
                        <option value="0">请选择类别</option>
                        @{IList<Shop.Wares.Models.WaresCategory> wcinfo = ViewBag.wcinfo;}
                        @foreach (var item in wcinfo)
                        {
                            if (item.CID == Model.CID)
                            {
                                <option value="@item.CID" selected="selected">@item.CName</option>
                            }
                            else
                            {
                                <option value="@item.CID">@item.CName</option>
                            }
                        }
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="SCID">子类别</label>
                <div class="col-lg-4">
                    <select class="form-control" name="SCID" id="SCID">
                        @{ IList<Shop.Wares.Models.SonWaresCategory> swcinfo = ViewBag.swcinfo;}
                        @foreach (var item in swcinfo)
                        {
                            if (item.SCID == Model.CID)
                            {
                                <option value="@item.SCID" selected="selected">@item.SCName</option>
                            }
                            else
                            {
                                <option value="@item.SCID">@item.SCName</option>
                            }
                        }
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="OPrice">原价</label>
                <div class="col-lg-4">
                    <input type="text" class="form-control" id="OPrice" name="OPrice" placeholder="原价" value="@Model.OPrice">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="PPrice">现价</label>
                <div class="col-lg-4">
                    <input type="text" class="form-control" id="PPrice" name="PPrice" placeholder="现价" value="@Model.PPrice">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="Stock">库存</label>
                <div class="col-lg-4">
                    <input type="number" class="form-control" name="Stock" id="Stock" placeholder="库存" min="1" value="@Model.Stock" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="Describe">商品描述</label>
                <div class="col-lg-8">
                    <input type="text" class="form-control" name="Describe" id="Describe" placeholder="商品描述" value="@Model.Describe" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="Url">原图片</label>
                <div class="col-lg-4">
                    <img src="@Model.Url" alt="商品图片" class="img-rounded" style="max-width:100px;max-height:100px;">
                    <input type="text" hidden="hidden" value="@Model.Url" name="OldUrl" id="OldUrl" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="Url">修改图片</label>
                <div class="col-lg-4">
                    <input id="Url" name="Url" type="file" class="file-loading" multiple data-show-caption="true">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="Url">原电子</label>
                <div class="col-lg-4">
                    <img src="@Model.bookLocation" alt="商品图片" class="img-rounded" style="max-width:100px;max-height:100px;">
                    <input type="text" hidden="hidden" value="@Model.bookLocation" name="OldBook" id="OldUrl" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label" for="Url">修改电子书</label>
                <div class="col-lg-4">
                    <input id="bookLocation" name="bookLocation" type="file" class="file-loading" multiple data-show-caption="true">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">商品介绍</label>
                <div class="col-lg-8">
                    @Html.Partial("_Editor")
                </div>
            </div>
            <div class="form-group">
                <div class="col-lg-2"></div>
                <div class="col-lg-4">
                    <input type="submit" class="btn btn-primary" value="修改" />
                </div>
                <div class="col-lg-4">
                    <input type="button" class="btn btn-primary" id="resetBtn" value="重置" />
                </div>
            </div>
        </form>
    </div>
    <script type="text/javascript">
        $(document).on('ready', function () {
            $("#Url").fileinput({
                language: 'zh', //设置语言
                uploadUrl: "", //上传的地址
                allowedFileExtensions: ['jpg', 'png', 'gif'],//接收的文件后缀
                showUpload: false, //是否显示上传按钮
                showCaption: false,//是否显示标题
                browseClass: "btn btn-primary", //按钮样式
                previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
            });
        });
        $(document).on('ready', function () {
            $("#bookLocation").fileinput({
                language: 'zh', //设置语言
                uploadUrl: "", //上传的地址
                allowedFileExtensions: ['txt','pdf'],//接收的文件后缀
                showUpload: false, //是否显示上传按钮
                showCaption: false,//是否显示标题
                browseClass: "btn btn-primary", //按钮样式
                previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
            });
        });
        $(document).ready(function () {
            $('#UpdateForm')
            .bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    WName: {
                        validators: {
                            notEmpty: {
                                message: '商品名不能为空'
                            },
                            stringLength: {
                                min: 1,
                                max: 15,
                                message: '商品名长度在1-15之间'
                            }
                        }
                    },
                    Describe: {
                        validators: {
                            notEmpty: {
                                message: '类别描述不能为空'
                            },
                            stringLength: {
                                min: 5,
                                max: 50,
                                message: '类别描述在5-50个字符之间'
                            }
                        }
                    },
                    OPrice: {
                        validators: {
                            notEmpty: {
                                message: '用户名不能为空'
                            },
                            regexp: {
                                regexp: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
                                message: '请输入正确的金额(最多输入两位小数)'
                            }
                        }
                    },
                    PPrice: {
                        validators: {
                            notEmpty: {
                                message: '用户名不能为空'
                            },
                            regexp: {
                                regexp: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
                                message: '请输入正确的金额(最多输入两位小数)'
                            }
                        }
                    },
                    Stock: {
                        validators: {
                            notEmpty: {
                                message: '库存不能为空'
                            },
                             regexp: {
                                regexp: /(^[1-9]([0-9]+)?$)/,
                                message: '请输入正确的数字'
                            },
                            callback: {
                                message: '库存不能超过100万',
                                callback: function (value, validator) {
                                    if (value > 1000000)
                                    {
                                        return false;
                                    }
                                    return true;
                                }
                            }
                        }
                    },
                    Describe: {
                        validators: {
                            notEmpty: {
                                message: '商品介绍不能为空'
                            },
                            stringLength: {
                                min: 1,
                                max: 50,
                                message: '商品介绍在1-50个字符之间'
                            }
                        }
                    },
                    CID: {
                        validators: {
                            callback: {
                                message: '请选择类别',
                                callback: function (value, validator) {
                                    var res = true;
                                    if (value == 0)
                                    {
                                        res = false;
                                    }
                                    return res;
                                }
                            },
                            callback: {
                                message: '该类别不存在子类别',
                                callback: function (value, validator) {
                                    var res = true;
                                    $.ajax({
                                        url: '@Url.Action("SWCount", "Wares")',
                                        type: 'post',
                                        dataType: 'text',
                                        async: false,
                                        data: { CID: value},
                                        success: function (data)
                                        {
                                            if (data == "NO")
                                            {
                                                res = false;
                                            }
                                        }
                                    });
                                    return res;
                                }
                            }
                        }
                    },
                    SCID: {
                        validators: {
                            callback: {
                                message: '请选择子类别',
                                callback: function (value, validator) {
                                    if (value == "NO")
                                    {
                                        return false;
                                    }
                                    return true;
                                }
                            }
                        }
                    },
                }
            });
        });
        $("#CID").change(function () {
            var select = $(this).val();
            $("#SCID").empty();
            if (select > 0)
            {
                 $.ajax({
                    url: '@Url.Action("Getswinfo", "Wares")',
                    type: 'post',
                    dataType: 'json',
                    data: { CID: select },
                    success: function (data) {
                        if (data != null) {
                            $.each(data, function (i, item) {
                                $("#SCID").append("<option value='" + item.SCID + "'>" + item.SCName + "</option>");   //为Select追加一个Option(下拉项)
                            });
                        }
                    }
                });
            }
        });
        //重置
        $('#resetBtn').click(function () {
            $('#UpdateForm').data('bootstrapValidator').resetForm(true);
            $('#UpdateForm')[0].reset();
            $("#CID").val("0");
            $("#SCID").empty();
        });
    </script>
</body>
</html>
